package corejava.corejava1.ch06.p05;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

/**
 * This program demonstrates anonymous inner classes.
 * 
 * @version 1.10 2004-02-27
 * @author Cay Horstmann
 */
public class Ch6_05_AnonymousInnerClassTest {
    public static void main(String[] args) {
        TalkingClock clock = new TalkingClock();
        clock.start(1000, true);

        // keep program running until user selects "Ok"
        JOptionPane.showMessageDialog(null, "Quit program?");
        System.exit(0);
    }
}

/**
 * A clock that prints the time in regular intervals.
 */
class TalkingClock {
    /**
     * Starts the clock.
     * 
     * @param interval
     *            the interval between messages (in milliseconds)
     * @param beep
     *            true if the clock should beep
     */
    public void start(int interval, final boolean beep) {
        ActionListener listener = new ActionListener() // 只创建这个类的一个对象，就不必命名了
        {
            public void actionPerformed(ActionEvent event) {
                Date now = new Date();
                System.out.println("At the tone, the time is " + now);
                if (beep)
                    Toolkit.getDefaultToolkit().beep();
            }
        };
        Timer t = new Timer(interval, listener);
        t.start();
    }
}
